package com.dimanche.library_base.utils;

import android.os.Handler;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author : Dimanche
 * date   : 2020/8/28 17:11
 * desc   :
 * version: 1.0
 */
public class ThreadPoolUtils {

    private volatile static ExecutorService mExecutorService;
    private static final Handler mHandler = new Handler();

    /**
     * 获取线程池单例
     * 双重锁机制
     *
     * @return
     */
    public static ExecutorService getExecutorService() {
        if (mExecutorService == null) {
            synchronized (ExecutorService.class) {
                if (mExecutorService == null) {
                    mExecutorService = Executors.newCachedThreadPool();
                }
            }
        }
        return mExecutorService;
    }

    /**
     * 获取handler
     *
     * @return
     */
    public static Handler getHandler() {

        return mHandler;
    }

    /**
     * 在子线程中执行任务
     *
     * @param runnable
     */
    public static void executorTask(Runnable runnable) {
        getExecutorService().execute(runnable);
    }

}
